package cn.piumnl.tool.mapeditor;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 * @author piumnl
 * @version 1.0.0
 * @since on 2017-06-02.
 */
public class MapEditor extends Application {

    public static final int CELL_WIDTH = 32;

    private Stage stage;
    private ScrollPane content;
    private MapConfig config;


    @Override
    public void start(Stage primaryStage) throws Exception {
        config = new MapConfig(CELL_WIDTH);

        BorderPane root = new BorderPane();
        root.setTop(loadMenus());
        content = new ScrollPane();
        content.setPrefSize(640, 480);
        root.setCenter(content);

        Scene scene = new Scene(root);
        this.stage = primaryStage;
        primaryStage.setScene(scene);
        primaryStage.setTitle("二维地图编辑器");
        primaryStage.show();
    }

    private Node loadMenus() {
        MenuBar bar = new MenuBar();

        // -----------------------------------------------------------------------------------------
        Menu file = new Menu("文件");
        MenuItem scene = new MenuItem("打开场景文件");
        MenuItem map = new MenuItem("打开地图文件");
        MenuItem save = new MenuItem("保存地图");
        file.getItems().addAll(scene, map, save);

        scene.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("图片文件", "*.png", "*.jpg"));
            File chooseFile = fileChooser.showOpenDialog(stage);
            if (chooseFile != null && chooseFile.isFile()) {
                try {
                    InputStream input = Files.newInputStream(chooseFile.toPath());
                    content.setContent(config.loadImg(new Image(input)));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        map.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("地图文件", "*.map"));
            File chooseFile = fileChooser.showOpenDialog(stage);
            if (chooseFile != null && chooseFile.isFile()) {
                try {
                    if (config.hasLoadImage()) {
                        config.loadMap(chooseFile);
                    } else {
                        config.alertNoMatch("请先选择场景文件！");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        save.setOnAction(event -> {
            FileChooser chooser = new FileChooser();
            chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("地图文件", "*.map"));
            File saveFile = chooser.showSaveDialog(stage);
            if (saveFile != null) {
                try {
                    config.save(saveFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        // -----------------------------------------------------------------------------------------
        Menu help = new Menu("帮助");
        MenuItem about = new MenuItem("关于");
        help.getItems().add(about);

        bar.getMenus().addAll(file, help);
        return bar;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
